package com.example.myapplication.login;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.example.myapplication.R;
import com.example.myapplication.login.dagger.DaggerLoginComponent;
import com.example.myapplication.login.dagger.LoginPresentModule;
import com.example.myapplication.login.model.User;
import com.example.myapplication.login.presenter.LoginPresenter;
import com.example.myapplication.login.view.LoginView;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends AppCompatActivity implements LoginView{

    @BindView(R.id.edit11)
    EditText edit1;
    @BindView(R.id.edit12)
    EditText edit2;
    @BindView(R.id.button11)
    Button button1;
    @BindView(R.id.button12)
    Button button2;
    @BindView(R.id.main_progress)
    ProgressBar mprogressBar;

    @Inject
    LoginPresenter loginPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        ButterKnife.bind(this);

        DaggerLoginComponent.builder()
                .loginPresentModule(new LoginPresentModule(this))
                .build().inject(this);
    }

    private void initView() {

    }

    @OnClick(R.id.button11)
    void login(){
        loginPresenter.login();
    }

    @OnClick(R.id.button12)
    void clear(){
        loginPresenter.clear();
    }

    @Override
    public String getUserName() {
        return edit1.getText().toString();
    }

    @Override
    public String getPassword() {
        return edit2.getText().toString();
    }

    @Override
    public void showLoading() {
        mprogressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideLoading() {
        mprogressBar.setVisibility(View.GONE);
    }

    @Override
    public void showSuccess(User user) {
        Toast.makeText(this,user.getUserName()+"登陆成功",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showFailedMsg(String s) {
        Toast.makeText(this,s,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void clearEditText() {
        edit1.setText("");
        edit2.setText("");
    }

    @Override
    public void setPresenter(LoginPresenter presenter) {
        loginPresenter = presenter;
    }
}
